Repository navigation
Conversation
…deferred iteration
When a deferred for loop's body reads a variable and then assigns it a
value that doesn't depend on it (`{{ a }}|{% set a = 7 %}`), eager
execution preserved the variable's pre-loop value twice: once in front
of the loop (correct) and again at the start of the loop body, which
reset it on every iteration. Default mode renders `0|7|`; the eager
output rendered `0|0|`. A loop variable with the same name as the
outer variable was overwritten the same way.
Only emit second-run prefix entries for variables that the prefix in
front of the loop doesn't already preserve.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Owner
Author
|
Pr opened on HubSpot/jinjava |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
EagerForTaghandles{% for %}in eager execution mode.When the loop's collection is deferred, as in
{% for i in deferred %}, it can't run the loop, so it writes the loop back out as template code to be rendered later.This pull request calls that first render the eager pass and the later render the second pass.
Problem
To find out which outer variables the loop body changes,
EagerForTagrenders the loop-body twice.The variables it changes are set in front of the loop, which is correct. But the second run's copy of those
settags was also written at the start of the loop body, so the variable was reset on every iteration.It only happens when the body reads a variable and then assigns it a value that doesn't depend on it.
foo = foo + 1, the case in the existingitDefersNodeWhenModifiedInForLoopfixture, takes a different path, which is why that fixture didn't catch it.Broken Examples
Eager output for the first example before this change. Note the second
setinside the loop:What this PR Does
In
EagerForTag.eagerInterpret, the loop body now only includessettags for variables that aren't already set in front of the loop. Assignments carry over between iterations again, and a loop variable with the same name as an outer variable is no longer overwritten. Eager output for the first example:Only eager output for deferred loops changes. No existing test expectations change.
Tests
EagerForTagTest:itKeepsAssignmentsAcrossIterationsOfDeferredLoopanditDoesNotOverwriteLoopVariableShadowingAssignedOuterVariable. Both fail onmasterand pass with this change.EagerForTagTest,ForTagTest,EagerTestandNonRevertingEagerTestall pass (445 tests). The fullmvn verifypasses.Results
{% for i in deferred %}{{ flag }}|{% set flag = 7 %}{% endfor %}0|0|0|7|{% for item in deferred %}{{ item }}|{% set item = 0 %}{% endfor %}0|0|1|2|🤖 Generated with Claude Code